import tornado.ioloop
import tornado.web
from pymongo import mongo_client
import json
import os.path
from bson import ObjectId

class JSONEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, ObjectId):
            return str(o)
        return json.JSONEncoder.default(self, o)

class ActionHandler(tornado.web.RequestHandler):
    def get(self):
        self.set_header("Content-Type", "charset=UTF-8")
        area = self.get_argument('area','福田区')
        for bean in db.col.find({'district':area}):
            print(bean)

class WebHandler(tornado.web.RedirectHandler):
    def get(self):
        print("index")
        self.render("index.html")

def make_app():
    return tornado.web.Application([
        (r"/job", ActionHandler),
        (r"/show", WebHandler),
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
    )

if __name__ == "__main__":
    conn = mongo_client.MongoClient("mongodb://localhost:27017/")
    db = conn.jobDB
    app = make_app()
    app.listen(8899)
    tornado.ioloop.IOLoop.current().start()
